# coding:utf-8
# Project:pyinterface
# author:wj
# time:2023/1/5 14:35
# Email:1305137785@qq.com
import requests,re
from jsonpath import jsonpath
from conf.settings import kfw_info
from tool.handle_MD import HandleMD5Tool
from tool.handle_attribute import HandleAttribute
from loguru import logger
class HandleCookie:
    def __init__(self):
        self.handle_md5 = HandleMD5Tool()
#   快服网登录:
    def handle_kfw_login(self):
        loginurl = kfw_info['login_url']
        data = {
                'account': kfw_info['user'],
                'password': self.handle_md5.Md5Tool(kfw_info['password'])
                }
        res = requests.post(url=loginurl,data=data)
        return res
#   快服网获取登录后的cookie
    def kfw_login_cookie(self):
        # 先获取token
        res_data = jsonpath(self.handle_kfw_login().json(),"$..data")[0]
        pattern = "token=(.*)"
        token=re.findall(pattern,res_data)[0]
        print(token)
        cookie_url = kfw_info['cookie_url']
        token_data = {'token': token}
        res = requests.get(url=cookie_url,params=token_data)
        print(type(res.request.headers),res.request.headers)
        cookies = res.request.headers['Cookie']
        # 将cookie设置成全局变量
        setattr(HandleAttribute,'kuaifuwang_cookie',cookies)
        logger.info(f'将快服网的cookie设置成全局变量{getattr(HandleAttribute,"kuaifuwang_cookie")}')
        return cookies




if __name__ == '__main__':
    HandleCookie().kfw_login_cookie()